import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

/**
 * The final step in the wizard-mode GUI where the user
 * is allowed to review the options that they have selected.
 * 
 * @author James Stanier
 */
public class ReviewGUI implements ActionListener {

	TocGUI wiz7;

	String outputFileName;
	String outputDir;
	String paperType;
	String variableOutput;
	String title;
	String author;
	int fontSize;
	boolean twoSidedPrinting;
	boolean inDepthSection;
	boolean keywordIndex;
	boolean codeIndex;
	boolean titlePage;
	boolean tableOfContents;

	JFrame window;
	JFrame prev;
	JButton nextButton;
	JButton backButton;

	ArrayList<File> inputFiles;

	/**
	 * Constructor.
	 * @param g The previous screen, TocGUI.
	 */
	public ReviewGUI(TocGUI g) {
		wiz7 = g;

		inputFiles = wiz7.getInputFiles();
		prev = wiz7.getWindow();
		outputFileName = wiz7.getOutputFileName();
		outputDir = wiz7.getOutputDir();
		paperType = wiz7.getPaperType();
		fontSize = wiz7.getFontSize();
		twoSidedPrinting = wiz7.isTwoSidedPrinting();
		inDepthSection = wiz7.isInDepthSection();
		keywordIndex = wiz7.isKeywordIndex();
		variableOutput = wiz7.getVariableOutput();
		codeIndex = wiz7.isCodeIndex();
		title = wiz7.getTitle();
		author = wiz7.getAuthor();
		titlePage = wiz7.isTitlePage();
		tableOfContents = wiz7.isTableOfContents();
		reviewWindow();
	}

	/**
	 * Configures and draws the screen.
	 */
	public void reviewWindow() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,650);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);


		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);

		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; Table of contents</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> Output &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>8. Review</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>Below are the options you have chosen " +
				"for your report. If these are correct, click 'Create report'. If not, please go back " +
		"and review your chosen options.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);

		// Display of chosen options
		JPanel optionPanel = new JPanel();
		optionPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		JLabel optionDisplay = new JLabel();

		String temp = "<HTML><font size=2>";
		temp += "Input files: ";

		for(int i = 0; i < inputFiles.size(); i++) {
			if(i > 0) {
				temp += ",";
			}
			temp += inputFiles.get(i).getName();
		}
		temp += "<br>";
		temp += "Output to: " + outputDir + "/" + outputFileName + ".tex<br><br>";
		
		temp += "Title page: " + titlePage + "<br>";
		if(!title.equals("")) {
			temp += "Title: " + title + "<br>";
			temp += "Author: " + author + "<br>";
		}
		
		temp += "Table of contents: " + tableOfContents + "<br>";
		temp += "Paper type: " + paperType + "<br>";
		temp += "Font size: " + fontSize + "<br>";
		temp += "Two sided printing: " + twoSidedPrinting + "<br><br>";
		temp += "In-depth section: " + inDepthSection + "<br>";
		temp += "Variable output: " + variableOutput + "<br>";
		temp += "Keyword index: " + keywordIndex + "<br>";
		temp += "Code index: " + codeIndex + "<br>";
		temp += "</HTML>";
		
		optionDisplay.setText(temp);
		optionPanel.add(optionDisplay);

		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Create report");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);

		window.add("North",top);
		window.add("Center",optionPanel);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
	}

	/**
	 * Actions to be taken on component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(backButton)) {
			window.setVisible(false);
			prev.setVisible(true);
		}
		else if(x.equals(nextButton)) {
			OutputHandler oh = new OutputHandler(this, inputFiles, window);
		}
	}

	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}

	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}

	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}

	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}

	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}

	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}

	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether there will be an in-depth section as a boolean.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}

	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether there will be a keyword index as a boolean.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}

	/**
	 * Get the type of variable output requested.
	 * @return Variable output status as a String.
	 */
	public String getVariableOutput() {
		return variableOutput;
	}

	/**
	 * Get whether a code index has been requested.
	 * @return Whether there will be a code index as a boolean.
	 */
	public boolean isCodeIndex() {
		return codeIndex;
	}

	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}

	/**
	 * Get the current author name.
	 * @return The author as a String.
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Get the current title.
	 * @return The title as a String.
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * Get whether the user has requested a title page.
	 * @return Whether a title page is to be included.
	 */
	public boolean isTitlePage() {
		return titlePage;
	}
	
	/**
	 * Get whether the user has requested a table of
	 * contents.
	 * @return Whether a table of contents is to be included.
	 */
	public boolean isTableOfContents() {
		return tableOfContents;
	}
}
